package com.netclass.meethealth.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.netclass.meethealth.R;
import com.netclass.meethealth.model.Sport;

import java.util.ArrayList;
import java.util.List;

/**
 * sport信息的适配器
 * <p/>
 * Created by luodong on 15/12/13.
 */
public class PartAdapter extends BaseAdapter {

    // 运动LIST
    private List<Sport> sports;
    // context
    private Context context;
    // 个人信息
    private String mPhone;

    public PartAdapter(Context context, List<Sport> sportList) {
        this.context = context;
        if (sportList != null) {
            sports = sportList;
        } else {
            sports = new ArrayList<>();
        }

        //测试
        mPhone = "123";
    }

    @Override
    public int getCount() {
        return sports.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_part, null);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.u_dis = (TextView) convertView.findViewById(R.id.u_dis);
            viewHolder.intro = (TextView) convertView.findViewById(R.id.intro);
            viewHolder.address = (TextView) convertView.findViewById(R.id.address);
            viewHolder.a_dis = (TextView) convertView.findViewById(R.id.a_dis);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
            viewHolder.limit = (TextView) convertView.findViewById(R.id.limit);
            viewHolder.btnApplyOrCancel = (Button) convertView.findViewById(R.id.apply_or_cancel);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            if (mPhone.equals(sports.get(position).getName())) {
                //自己的发布的
                viewHolder.name.setText("ME");
                viewHolder.u_dis.setText("我发起的");
                viewHolder.btnApplyOrCancel.setVisibility(View.GONE);
            } else {
                viewHolder.name.setText(sports.get(position).getName());
                viewHolder.u_dis.setText(sports.get(position).getUser_distance() + "");
                if (sports.get(position).isApplyed()) {
                    viewHolder.btnApplyOrCancel.setText("我已报名");
                    viewHolder.btnApplyOrCancel.setClickable(false);
                    viewHolder.btnApplyOrCancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (listener != null) {
                                listener.click(position, false);
                                ((Button)v).setText("报名");
                            }
                        }
                    });
                } else {
                    viewHolder.btnApplyOrCancel.setText("报名");
                    viewHolder.btnApplyOrCancel.setClickable(true);
                    viewHolder.btnApplyOrCancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (listener != null) {
                                listener.click(position, true);
                                ((Button)v).setText("已报名");
                            }
                        }
                    });
                }

            }

            viewHolder.intro.setText(sports.get(position).getIntro());
            viewHolder.a_dis.setText(sports.get(position).getPla_distance() + "");
            viewHolder.time.setText(sports.get(position).getDelay() + "");
            viewHolder.address.setText(sports.get(position).getPlace());
            viewHolder.limit.setText(sports.get(position).getLimit() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    /**
     * 刷新所有数据
     *
     * @param sports
     */
    public void refresh(List<Sport> sports) {
        this.sports = sports;
        notifyDataSetChanged();
    }

    class ViewHolder {
        private TextView name;//发起人姓名
        private TextView u_dis;//距离发起人的距离
        private TextView intro;//活动简介
        private TextView address;//活动地点
        private TextView a_dis;//活动地点的距离
        private TextView time;// 活动时间
        private TextView limit;//报名人数
        private Button btnApplyOrCancel;//报名或取消按钮
    }

    private OnItemButtonClickListener listener;

    /**
     * 设置监听器
     *
     * @param listener
     */
    public void setOnItemButtonClickListener(OnItemButtonClickListener listener) {
        this.listener = listener;
    }

    /**
     * 点击列表项中的按钮的接口
     */
    public interface OnItemButtonClickListener {
        public void click(int position, boolean join);
    }
}
